package com.example.demo.deepseek.controller;


import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.messages.AssistantMessage;
import org.springframework.ai.chat.messages.SystemMessage;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zwg
 * @date 2025/2/17 15:22
 */
@Slf4j
@Tag(name = "公共接口")
@RestController
@RequestMapping(path = "/deepseek")
public class DeepSeekController {


    private final ChatClient chatClient;

    public DeepSeekController(ChatClient.Builder builder){
        this.chatClient = builder.build();
    }

    @GetMapping("/")
    public String index() {

        return "this is open ai";
    }


    @Operation(summary = "testChat")
    @GetMapping("/testChat")
    public String testChat(@RequestParam(defaultValue = "user") String mType, @RequestParam(defaultValue = "hello open ai") String message) {
        ChatClient.ChatClientRequestSpec prompt = chatClient.prompt();

        log.info("prompt:{}",prompt);
        if("system".equals(mType)){
            prompt.messages(new SystemMessage(message));
        } else if ("assistant".equals(mType)) {
            prompt.messages(new AssistantMessage(message));
        }else{
            prompt.messages(new UserMessage(message));
        }
        try {
            ChatClient.CallResponseSpec response = prompt.call();
            if (response != null) {
                return response.content();
            } else {
                return "not response";
            }
        }catch (Exception e) {
            log.error("",e);
            return e.getMessage();
        }

    }


}
